package com.example.gitpart03;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class DingDanFragment extends Fragment {
    private List<Order> orderList = new ArrayList<>();
    private OrderAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dingdan, container, false);
        ListView orderListView = view.findViewById(R.id.order_listview);
        adapter = new OrderAdapter(requireContext(), orderList);
        orderListView.setAdapter(adapter);
        return view;
    }
    public void addOrder(String productName, int productImageResId) {
        orderList.add(new Order(productImageResId, productName));
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
    private static class OrderAdapter extends ArrayAdapter<Order> {
        public OrderAdapter(android.content.Context context, List<Order> orderList) {
            super(context, 0, orderList);
        }
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.dingdan_list, parent, false);
            }
            Order item = getItem(position);
            if (item != null) {
                ImageView productImage = convertView.findViewById(R.id.order_product_image);
                TextView productName = convertView.findViewById(R.id.order_product_name);
                TextView orderStatus = convertView.findViewById(R.id.order_status);
                productImage.setImageResource(item.getProductImage());
                productName.setText(item.getProductName());
                orderStatus.setText("已支付");
            }
            return convertView;
        }
    }
}